package com.bfly.mall.advert.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.mall.advert.entity.Advert;
import com.bfly.mall.advert.dto.AdvertListDTO;
import com.bfly.mall.advert.service.IAdvertService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 广告API
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 22:48
 */
@RestController
@RequestMapping(value = "/api/advert")
public class AdvertApiController extends BaseController {

    @Autowired
    private IAdvertService advertService;

    /**
     * 广告列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/18 22:48
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response, @RequestParam("type") int type) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("type", type);
        param.put("is_enable", true);
        List<Advert> adverts = advertService.getList(param);
        List<AdvertListDTO> list = new ArrayList<>();
        if (adverts != null) {
            for (Advert advert : adverts) {
                AdvertListDTO dto = new AdvertListDTO();
                BeanUtils.copyProperties(advert, dto);
                list.add(dto);
            }
            Collections.sort(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }
}